package com.huishu.tcm.util;

import cn.hutool.core.util.ArrayUtil;
import com.huishu.tcm.entity.Comparative;
import com.huishu.tcm.entity.User;

import java.lang.reflect.Field;
import java.util.List;

public class ObjUtil {

    // 将对照组对象复制为User对象，导出数据时使用
    public static void comparative2User(List<User> userList, List<Comparative> comparativeList) throws ClassNotFoundException, IllegalAccessException {
        User user;
        for(Comparative comparative: comparativeList){
            user = new User();
            Class userClass = Class.forName("com.huishu.tcm.entity.User");
            Field[] userFields = ArrayUtil.addAll(userClass.getDeclaredFields(),userClass.getSuperclass().getDeclaredFields());
            Class comparativeClass = Class.forName("com.huishu.tcm.entity.Comparative");
            Field[] comparativeFields = ArrayUtil.addAll(comparativeClass.getDeclaredFields(),comparativeClass.getSuperclass().getDeclaredFields());
            for(Field userField: userFields){
                for(Field comparativeField: comparativeFields){
                    if(userField.getName().equals(comparativeField.getName())){
                        userField.setAccessible(true);
                        comparativeField.setAccessible(true);
                        userField.set(user,comparativeField.get(comparative));
                        break;
                    }
                }
            }
            userList.add(user);
        }
    }
}
